package com.tongtech.system.controller;

import com.tongtech.common.core.constant.UserConstants;
import com.tongtech.common.core.utils.StringUtils;
import com.tongtech.common.core.web.controller.BaseController;
import com.tongtech.common.core.web.domain.AjaxResult;
import com.tongtech.common.log.annotation.Log;
import com.tongtech.common.log.enums.BusinessType;
import com.tongtech.common.security.annotation.RequiresPermissions;
import com.tongtech.common.security.utils.SecurityUtils;
import com.tongtech.system.domain.WorkOrderCategory;
import com.tongtech.system.service.IWorkOrderCategoryService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工单类型信息
 *
 * @author xingyc
 */
@RestController
@RequestMapping("/workOrderCategory")
public class WorkOrderCategoryController extends BaseController
{
    @Autowired
    private IWorkOrderCategoryService workOrderCategoryService;

    /**
     * 获取工单类型列表
     */
    @RequiresPermissions("basicdata:workOrderCategory:list")
    @GetMapping("/list")
    public AjaxResult list(WorkOrderCategory workOrderCategory)
    {
        List<WorkOrderCategory> workOrderCategorys = workOrderCategoryService.selectWorkOrderCategoryList(workOrderCategory);
        return success(workOrderCategorys);
    }

    /**
     * 查询工单类型列表（排除节点）
     */
    @RequiresPermissions("basicdata:workOrderCategory:list")
    @GetMapping("/list/exclude/{workOrderCategoryId}")
    public AjaxResult excludeChild(@PathVariable(value = "workOrderCategoryId", required = false) Long workOrderCategoryId)
    {
        List<WorkOrderCategory> workOrderCategorys = workOrderCategoryService.selectWorkOrderCategoryList(new WorkOrderCategory());
        workOrderCategorys.removeIf(p -> p.getWorkOrderCategoryId().intValue() == workOrderCategoryId || ArrayUtils.contains(StringUtils.split(p.getAncestors(), ","), workOrderCategoryId + ""));
        return success(workOrderCategorys);
    }

    /**
     * 根据工单类型编号获取详细信息
     */
    @RequiresPermissions("basicdata:workOrderCategory:query")
    @GetMapping(value = "/{workOrderCategoryId}")
    public AjaxResult getInfo(@PathVariable Long workOrderCategoryId)
    {
        return success(workOrderCategoryService.selectWorkOrderCategoryById(workOrderCategoryId));
    }

    /**
     * 新增工单类型
     */
    @RequiresPermissions("basicdata:workOrderCategory:add")
    @Log(title = "工单类型管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WorkOrderCategory workOrderCategory)
    {
        if (!workOrderCategoryService.checkWorkOrderCategoryNameUnique(workOrderCategory))
        {
            return error("新增工单类型'" + workOrderCategory.getWorkOrderCategoryName() + "'失败，工单类型名称已存在");
        }
        workOrderCategory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(workOrderCategoryService.insertWorkOrderCategory(workOrderCategory));
    }

    /**
     * 修改工单类型
     */
    @RequiresPermissions("basicdata:workOrderCategory:edit")
    @Log(title = "工单类型管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody WorkOrderCategory workOrderCategory)
    {
        Long workOrderCategoryId = workOrderCategory.getWorkOrderCategoryId();
        if (!workOrderCategoryService.checkWorkOrderCategoryNameUnique(workOrderCategory))
        {
            return error("修改工单类型'" + workOrderCategory.getWorkOrderCategoryName() + "'失败，工单类型名称已存在");
        }
        else if (workOrderCategory.getParentId().equals(workOrderCategoryId))
        {
            return error("修改工单类型'" + workOrderCategory.getWorkOrderCategoryName() + "'失败，上级工单类型不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, workOrderCategory.getStatus()) && workOrderCategoryService.selectNormalChildrenWorkOrderCategoryById(workOrderCategoryId) > 0)
        {
            return error("该工单类型包含未停用的子工单类型！");
        }
        workOrderCategory.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(workOrderCategoryService.updateWorkOrderCategory(workOrderCategory));
    }

    /**
     * 删除工单类型
     */
    @RequiresPermissions("basicdata:workOrderCategory:remove")
    @Log(title = "工单类型管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workOrderCategoryId}")
    public AjaxResult remove(@PathVariable Long workOrderCategoryId)
    {
        if (workOrderCategoryService.hasChildByWorkOrderCategoryId(workOrderCategoryId))
        {
            return warn("存在下级工单类型,不允许删除");
        }
        return toAjax(workOrderCategoryService.deleteWorkOrderCategoryById(workOrderCategoryId));
    }

    /**
     * 获取部门树列表
     */
    @RequiresPermissions("basicdata:workOrderCategory:list")
    @GetMapping("/workOrderCategoryTree")
    public AjaxResult workOrderCategoryTree(WorkOrderCategory workOrderCategory)
    {
        return success(workOrderCategoryService.selectWorkOrderCategoryTreeList(workOrderCategory));
    }
}
